#!/usr/bin/env bash
#
# Copyright (c) 2020-2021, eBay Inc.
# Initially written by Jon Degenhardt
#
# License: Boost Licence 1.0 (http://boost.org/LICENSE_1_0.txt)
#
# -------------------------------------------------------------
#
# This file contains a sample implementation of the 'tsv-sort' script
# described on the Tips and Tricks page in the eBay tsv-utils repository:
# https://github.com/eBay/tsv-utils/blob/master/docs/TipsAndTricks.md
#

command_name="sort"
mac_gnu_name="gsort"

if [ "$(uname)" == "Darwin" ] && type $mac_gnu_name &>/dev/null ; then
   command_name=$mac_gnu_name
fi
if [ "$1" == "--help" ]; then
    script_name=$(basename "$0")
    echo ""
    echo "$script_name runs '$command_name' using TAB as the field delimiter and the buffer"
    echo "size set to 2G. All arguments are forwared to '$command_name'. Example:"
    echo ""
    echo "    tsv-sort data.tsv -k1,1 -k3,3"
    echo ""
    echo "This sorts data.tsv using the 1st and 3rd fields as keys."
    echo "Run '$command_name --help' for more information."
    echo ""
    exit 0
fi
$command_name -t $'\t' --buffer-size=2G "$@"
